
//Title:        RMI Chat Program
//Version:
//Copyright:    Copyright (c) 1999
//Author:       Chris Casey
//Company:      UCLan
//Description:  A Chat Server and Client using RMI
package rmichat;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.*;
import java.util.*;
import java.io.*;
import java.net.*;

public class ChatServerImpl extends UnicastRemoteObject implements ChatServer {
    
    Vector<rmichat.ChatListener> listeners = new Vector<rmichat.ChatListener>();
    static ClassFileServer fileServer = null;
    
    public ChatServerImpl() throws RemoteException {
    }
    
    public static void main(String[] args) {
        
        try {
            
            String defpath = System.getProperty("user.dir");
            
            
            //Create and install a security manager
            if (System.getSecurityManager() == null)
                System.setSecurityManager(new RMISecurityManager());
            ChatServerImpl chatServerImpl = new ChatServerImpl();
            // run a local mini-web server
            fileServer = new ClassFileServer(1999,defpath+"\\build\\classes");
            // Bind this object instance to the name "ChatServer"
            
            try {
                java.rmi.registry.LocateRegistry.createRegistry(1099);
                System.out.println("Starting registry");
            } catch (RemoteException ex) {
                System.out.println("Error starting registry");
            }
            
            
            Naming.rebind("//localhost/ChatServer", chatServerImpl);
            System.out.println("ChatServer bound in registry");
            
        } catch (RemoteException ex){
            System.out.println(ex.getMessage());
        } catch (MalformedURLException ex){
            System.out.println(ex.getMessage());
        } catch (IOException ex){
            System.out.println(ex.getMessage());
        }
        
        System.out.println("Press a key to close the server");
        try {
            int b = System.in.read();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (fileServer != null){
            System.out.println("Closing the server in six seconds");
            fileServer.requestClose();
            try {
                Thread.sleep(6000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            System.out.println("Closing the server");
            System.exit(0);
        }
        
        
    }
    
    public void removeChatListener(ChatListener cl) throws RemoteException{
        System.out.println("remove chat listener");
        listeners.remove(cl);
    }
    
    public void addChatListener(ChatListener cl) throws RemoteException {
        System.out.println("add chat listener");
        listeners.add(cl);
    }
    
    
    
    public void sendMessage(String author, String message)  {
        System.out.println("Sent Message: "+message);
        
        Enumeration elements = listeners.elements();
        while (elements.hasMoreElements()){
            ChatListener cl = (ChatListener) (elements.nextElement());
            try {
                cl.messageReceived(author, message);
            } catch (RemoteException ex) {
                listeners.remove(cl);
            }
        }
    }

}
